package com.newworld.basketballfire.main.dao;

import com.newworld.basketballfire.main.bean.ActivityListParam;
import com.newworld.basketballfire.main.dao.mapper.ActivityMapper;
import com.newworld.basketballfire.main.dao.mapper.ActivityRecordMapper;
import com.newworld.basketballfire.main.dao.mapper.DuesMapper;
import com.newworld.basketballfire.main.dao.mapper.UserMapper;
import com.newworld.basketballfire.service.model.Activity;
import com.newworld.basketballfire.service.model.ActivityRecord;
import com.newworld.basketballfire.service.model.Dues;
import com.newworld.basketballfire.service.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by George on 2019/3/31.
 * ActivityDao
 */
@Component
public class ActivityDao {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DuesMapper duesMapper;

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Transactional
    public void createActivity(Activity activity, List<User> users, List<Dues> duesList) {
        activityMapper.createActivity(activity);
        userMapper.updateUserListDues(users);
        duesMapper.addListDues(duesList);
        activityRecordMapper.addListActivityRecord(activity.getRecords());
    }

    @Transactional
    public void updateActivity(Activity activity, List<User> users, List<Dues> duesList, List<Dues> deleteDuesList, List<ActivityRecord> deleteRecords) {
        activityRecordMapper.deleteActivityRecordList(deleteRecords);
        duesMapper.deleteDuesList(deleteDuesList);
        activityMapper.updateActivity(activity);
        userMapper.updateUserListDues(users);
        duesMapper.addListDues(duesList);
        activityRecordMapper.addListActivityRecord(activity.getRecords());
    }

    public Activity getActivity(String id) {
        return activityMapper.getActivity(id);
    }

    public List<Activity> getActivityList(ActivityListParam activityListParam) {
        return activityMapper.getActivityList(activityListParam);
    }

    public int getActivityListTotalCnt(ActivityListParam activityListParam) {
        return activityMapper.getActivityListTotalCnt(activityListParam);
    }

    @Transactional
    public void deleteActivity(Activity activity, List<User> users, List<Dues> duesList) {
        userMapper.updateUserListDues(users);
        activityRecordMapper.deleteActivityRecord(activity);
        duesMapper.deleteDuesList(duesList);
        activityMapper.deleteActivity(activity);
    }
}
